<%@ page import="cn.js.fan.util.ParamUtil" %>
<%@ page import="com.redmoon.oa.person.UserDesktopSetupDb" %>
<%@ page import="com.redmoon.oa.flow.FormDb" %>
<%@ page import="com.redmoon.oa.visual.FormDAO" %>
<%@ page import="com.redmoon.oa.visual.ModuleSetupDb" %>
<%@ page import="com.redmoon.oa.sys.DebugUtil" %>
<%@ page import="com.redmoon.oa.flow.FormField" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.redmoon.oa.visual.ModuleUtil" %>
<%@ page import="cn.js.fan.util.ErrMsgException" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="cn.js.fan.util.StrUtil" %>
<%@ page import="com.redmoon.oa.chart.ChartUtil" %>
<%@ page contentType="text/html;charset=utf-8" %>
<%
    int id = ParamUtil.getInt(request, "id", -1);
    UserDesktopSetupDb udsd = new UserDesktopSetupDb();
    udsd = udsd.getUserDesktopSetupDb(id);

    long chartId = StrUtil.toLong(udsd.getMetaData(), -1);
    if (chartId == -1) {
        out.print("未设置关联的图形");
        DebugUtil.e(getClass(), "桌面：", udsd.getTitle() + " 未设置关联的图形！");
        return;
    }

    FormDb fdChart = new FormDb();
    fdChart = fdChart.getFormDb(ChartUtil.CONFIG_CHART);
    FormDAO fdaoChart = new FormDAO();
    fdaoChart = fdaoChart.getFormDAO(chartId, fdChart);
    String title = fdaoChart.getFieldValue("title");
    String fieldName = fdaoChart.getFieldValue("field_name");
    String moduleCode = fdaoChart.getFieldValue("module_code");
    boolean isAaverage = "1".equals(fdaoChart.getFieldValue("is_average"));

    ModuleSetupDb msd = new ModuleSetupDb();
    msd = msd.getModuleSetupDb(moduleCode);
    if (msd == null) {
        DebugUtil.e(getClass(), "getModuleSetupDb", "模块：" + moduleCode + "不存在！");
        out.print("模块：" + moduleCode + "不存在！");
        return;
    }
    String formCode = msd.getString("form_code");

    FormDb fd = new FormDb();
    fd = fd.getFormDb(formCode);
    FormField ff = fd.getFormField(fieldName);

    JSONArray arr = null;
    try {
        arr = ChartUtil.getDataForChart(request, msd, fd, ff);
    } catch (ErrMsgException e) {
        out.print(e.getMessage());
        return;
    }
%>
<script>
    // 实例化一个JS数组
    var xItems = new Array();
    var yItems = new Array();
    // 循环遍历Java数组
    <%
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = arr.getJSONObject(i);
    %>
            // 将Java数组中的值赋给JS数组
    xItems[<%=i%>] = '<%=json.getString("name")%>';
    yItems[<%=i%>] = '<%=json.getString("value")%>';
    <%
         }
    %>
</script>
<div id="drag_<%=id%>" class="portlet drag_div bor ibox" style="padding:0;">
    <div id="drag_<%=id%>_h" class="drag-h" style="height:3px;padding:0;margin:0;font-size:1px"></div>
    <div class="ibox-title">
        <div class="chart-title">
            <h5>
                <i class="fa <%=udsd.getIcon()%>"></i>
                <span><%=udsd.getTitle()%></span>
            </h5>
        </div>
    </div>
    <div class="portlet_content ibox-content">
        <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
        <div id="barBox<%=id%>" style="width: 380px;height:210px;"></div>
        <script type="text/javascript">
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById("barBox<%=id%>"));
            // 指定图表的配置项和数据
            var option = {
                /*title: {
                    text: '漏斗图',
                    subtext: '纯属虚构'
                },*/
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c}%"
                },
                /*toolbox: {
                    feature: {
                        dataView: {readOnly: false},
                        restore: {},
                        saveAsImage: {}
                    }
                },*/
                /*legend: {
                    data: ['展现','点击','访问','咨询','订单']
                },*/
                series: [
                    {
                        name:'漏斗图',
                        type:'funnel',
                        left: '10%',
                        top: 30,
                        //x2: 80,
                        bottom: 30,
                        width: '80%',
                        // height: {totalHeight} - y - y2,
                        min: 0,
                        max: 100,
                        minSize: '0%',
                        maxSize: '100%',
                        sort: 'descending',
                        gap: 2,
                        label: {
                            show: true,
                            position: 'inside'
                        },
                        labelLine: {
                            length: 10,
                            lineStyle: {
                                width: 1,
                                type: 'solid'
                            }
                        },
                        itemStyle: {
                            borderColor: '#fff',
                            borderWidth: 1
                        },
                        emphasis: {
                            label: {
                                fontSize: 20
                            }
                        },
                        data: <%=arr%>
                        /*data: [
                            {value: 60, name: '访问'},
                            {value: 40, name: '咨询'},
                            {value: 20, name: '订单'},
                            {value: 80, name: '点击'},
                            {value: 100, name: '展现'}
                        ]*/
                    }
                ]
            };

            myChart.setOption(option);
        </script>
    </div>
</div>
